﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FormPipeDiameterMark : System.Windows.Forms.Form
	{
		public bool BeAutoCancel { get; set; }

		public bool UseForVrvLN { get; set; }

		public FormPipeDiameterMark()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{9D41099C-05CF-423D-AD8D-17FC33710B44}", null);
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = new HVACUserSetting();
			this.LoadAllImages();
			this.pictureBox1.BackColor = System.Drawing.Color.White;
			this.markWay = DiameterMarkWay.SingleUp;
			this.BeAutoCancel = false;
			this.beFirstLoad = true;
			this.UseForVrvLN = false;
		}

		public void SetControlStatueByView(bool bePlane)
		{
			if (!bePlane)
			{
				this.rdoHaveLeaderLine.Checked = false;
				this.rdoNoLeaderLine.Checked = true;
				this.rdoHaveLeaderLine.Enabled = false;
				return;
			}
			this.rdoHaveLeaderLine.Enabled = true;
		}

		private void FormPipeDiameterMark_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			Dictionary<string, string> pipeDiameterMark = this.userSetting.GetPipeDiameterMark();
			this.InitialInterface(pipeDiameterMark);
			this.SettingPreviewImage();
			this.beFirstLoad = false;
			base.KeyPreview = true;
			this.UpdateLeadDimCtrl();
		}

		private void LoadAllImages()
		{
			this.dicAllImages = new Dictionary<FormPipeDiameterMark.ImageKey, Image>();
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.MuiltyDown, YArchitech.HVAC.Properties.Resources.MuiltyDown);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.MuiltyUp, YArchitech.HVAC.Properties.Resources.MuiltyUp);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.MultyLine, YArchitech.HVAC.Properties.Resources.MultyLine);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.SingleDown, YArchitech.HVAC.Properties.Resources.SingleDown);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.SingleLineDown, YArchitech.HVAC.Properties.Resources.SingleLineDown);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.SingleLineUp, YArchitech.HVAC.Properties.Resources.SingleLineUp);
			this.dicAllImages.Add(FormPipeDiameterMark.ImageKey.SingleUp, YArchitech.HVAC.Properties.Resources.SingleUp);
		}

		private void InitialInterface(Dictionary<string, string> dicFaceInfos)
		{
			foreach (KeyValuePair<string, string> keyValuePair in dicFaceInfos)
			{
				string key = keyValuePair.Key;
				if (!(key == "Diameter"))
				{
					if (!(key == "DiameterValue"))
					{
						if (!(key == "MarkWay"))
						{
							if (!(key == "MarkStyle"))
							{
								if (key == "MarkLocation")
								{
									this.SelectedRadioButton(this.rdoUpLeader, this.rdoDownLeader, keyValuePair.Value);
								}
							}
							else
							{
								this.SelectedRadioButton(this.rdoHaveLeaderLine, this.rdoNoLeaderLine, keyValuePair.Value);
							}
						}
						else
						{
							this.SelectedRadioButton(this.rdoSingleSelect, this.rdoMuiltSelect, keyValuePair.Value);
						}
					}
					else
					{
						this.cmbDiameter.Text = keyValuePair.Value;
					}
				}
				else
				{
					this.ckbDiameter.Checked = Convert.ToBoolean(keyValuePair.Value);
					if (this.ckbDiameter.Checked)
					{
						this.cmbDiameter.Enabled = true;
					}
					else
					{
						this.cmbDiameter.Enabled = false;
					}
				}
			}
		}

		private void SelectedRadioButton(RadioButton rdoBtn1, RadioButton rdoBtn2, string defaultText)
		{
			if (defaultText.Equals(rdoBtn1.Text))
			{
				rdoBtn1.Checked = true;
				rdoBtn2.Checked = false;
				return;
			}
			rdoBtn1.Checked = false;
			rdoBtn2.Checked = true;
		}

		private void SettingPreviewImage()
		{
			if (this.rdoHaveLeaderLine.Checked)
			{
				if (this.rdoSingleSelect.Checked)
				{
					this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.SingleLineUp];
					this.markWay = DiameterMarkWay.SingleLine;
					return;
				}
				if (this.rdoMuiltSelect.Checked)
				{
					this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.MultyLine];
					this.markWay = DiameterMarkWay.MultyLine;
					return;
				}
			}
			else if (this.rdoSingleSelect.Checked)
			{
				if (this.rdoUpLeader.Checked)
				{
					this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.SingleUp];
					this.markWay = DiameterMarkWay.SingleUp;
					return;
				}
				this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.SingleDown];
				this.markWay = DiameterMarkWay.SingleDown;
				return;
			}
			else
			{
				if (this.rdoUpLeader.Checked)
				{
					this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.MuiltyUp];
					this.markWay = DiameterMarkWay.MuiltyUp;
					return;
				}
				this.pictureBox1.Image = this.dicAllImages[FormPipeDiameterMark.ImageKey.MuiltyDown];
				this.markWay = DiameterMarkWay.MuiltyDown;
			}
		}

		private void SaveInterFaceInfo()
		{
			this.userSetting.WritePipeDiameterMark(this.GetDefaultValue());
		}

		private Dictionary<string, string> GetDefaultValue()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Diameter", this.ckbDiameter.Checked.ToString());
			try
			{
				dictionary.Add("DiameterValue", Convert.ToInt32(this.cmbDiameter.Text).ToString());
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				dictionary.Add("DiameterValue", "25");
			}
			dictionary.Add("MarkWay", this.rdoSingleSelect.Checked ? this.rdoSingleSelect.Text : this.rdoMuiltSelect.Text);
			dictionary.Add("MarkStyle", this.rdoHaveLeaderLine.Checked ? this.rdoHaveLeaderLine.Text : this.rdoNoLeaderLine.Text);
			dictionary.Add("MarkLocation", this.rdoUpLeader.Checked ? this.rdoUpLeader.Text : this.rdoDownLeader.Text);
			return dictionary;
		}

		private void rdoSingleSelect_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoHaveLeaderLine.Checked)
			{
				this.hyGrpLocation.Enabled = false;
			}
			else
			{
				this.hyGrpLocation.Enabled = true;
			}
			this.SettingPreviewImage();
			RadioButton radioButton = sender as RadioButton;
			if (radioButton == null || this.beFirstLoad)
			{
				return;
			}
			if ((!this.BeAutoCancel || !this.UseForVrvLN) && radioButton.Checked)
			{
				this.BeAutoCancel = true;
				this.ChangeOperateState();
			}
			this.UpdateLeadDimCtrl();
		}

		private void FormPipeDiameterMark_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.SaveInterFaceInfo();
		}

		public DiameterMarkWay GetMarkWay()
		{
			return this.markWay;
		}

		public bool BeSettingDiamter()
		{
			return this.ckbDiameter.Checked;
		}

		public double GetSettingDiameter()
		{
			double num = 25.0;
			try
			{
				num = Convert.ToDouble(this.cmbDiameter.Text);
			}
			catch (Exception)
			{
				num = 25.0;
			}
			return num / 304.8;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			if (this.UseForVrvLN)
			{
				FormPipeDiameterMark.SendMessage(CmdDimLNPipeDN.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			FormPipeDiameterMark.SendMessage(PipeDiameterMark.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormPipeDiameterMark.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormPipeDiameterMark.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void FormPipeDiameterMark_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.BeAutoCancel = false;
			this.ChangeOperateState();
		}

		private void FormPipeDiameterMark_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		private void btnSetting_Click(object sender, EventArgs e)
		{
			FormTotalMarkSetting formTotalMarkSetting = new FormTotalMarkSetting(ShowMarkType.Diameter);
			if (this.rdoHaveLeaderLine.Checked)
			{
				formTotalMarkSetting.SetDrawSlidIsEnabled(true);
			}
			if (this.rdoMuiltSelect.Checked && this.rdoNoLeaderLine.Checked)
			{
				formTotalMarkSetting.SetDrawSlashEnable(true);
				formTotalMarkSetting.SetDiameterOtherEnabled(true);
			}
			else
			{
				formTotalMarkSetting.SetDiameterOtherEnabled(false);
			}
			formTotalMarkSetting.SetTabAndGroupEnable();
			formTotalMarkSetting.SetPipeDiameterShowState();
			formTotalMarkSetting.ShowDialog();
			formTotalMarkSetting.Close();
		}

		private void cmbDiameter_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cmbDiameter_TextChanged(object sender, EventArgs e)
		{
		}

		private void ckbDiameter_CheckedChanged(object sender, EventArgs e)
		{
			if (this.ckbDiameter.Checked)
			{
				this.cmbDiameter.Enabled = true;
				this.rdoMuiltSelect.Enabled = false;
				this.rdoSingleSelect.Checked = true;
				return;
			}
			this.cmbDiameter.Enabled = false;
			this.rdoMuiltSelect.Enabled = true;
		}

		private void FormPipeDiameterMark_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.BeAutoCancel = false;
				this.ChangeOperateState();
			}
		}

		private void cmbDiameter_Validated(object sender, EventArgs e)
		{
		}

		private void cmbDiameter_MouseLeave(object sender, EventArgs e)
		{
			try
			{
				int num = Convert.ToInt32(this.cmbDiameter.Text);
				if (num < 15 || num > 500)
				{
					YJKMessageBox.Warning("输入管径范围应该在[15,500]区间内！");
					this.cmbDiameter.Focus();
					this.cmbDiameter.SelectAll();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Warning("输入管径只能为正整数！");
				this.cmbDiameter.Focus();
				this.cmbDiameter.SelectAll();
			}
		}

		private void UpdateLeadDimCtrl()
		{
			if (!this.UseForVrvLN)
			{
				return;
			}
			if (this.rdoMuiltSelect.Checked)
			{
				this.rdoNoLeaderLine.Checked = true;
			}
			this.rdoHaveLeaderLine.Enabled = this.rdoSingleSelect.Checked;
		}

		private HVACUserSetting userSetting;

		private Dictionary<FormPipeDiameterMark.ImageKey, Image> dicAllImages;

		private DiameterMarkWay markWay = DiameterMarkWay.SingleUp;

		private bool beFirstLoad = true;

		private ControlInfoManager m_thisManger;

		private enum ImageKey
		{
			MuiltyDown,
			MuiltyUp,
			MultyLine,
			SingleDown,
			SingleLineDown,
			SingleLineUp,
			SingleUp
		}
	}
}
